clear all
set more off
capture log close
set matsize 11000 

*---- Set Directory ----*
global rootDir = "/Users/AliceZhang/Dropbox/Research_Columbia/Renewables Voting (Urpelainen Zhang)/JOP/UZ_JOP2021_Replication"
global dataDir = "Data"
global resultDir = "Results"
global logDir = "Analysis/logSTATA"
global graphDir = "Results/Figures"

*---- Log File Path ----*
cd "$rootDir/$logDir"
log using 009_analysis_CCES, replace


*******************************************************************************
/*				          URPELAINEN & ZHANG 2021	        				 */
*******************************************************************************

/* 

File Name:    	009_analysis_CCES.do

By:				Alice Tianbo Zhang (alice.tianbo.zhang@gmail.com)

Last Edited:  	10/14/2021

Purpose:		

Data Used:      votes_wind_panel_CCES.dta

Program Used:   - reghdfe -

*/


*******************************************************************************
/*                	     			TABLE A22		 		  			     */
*******************************************************************************
cd "$rootDir/$dataDir/Final"
use votes_wind_panel_CCES.dta, clear

** Create instrument and fixed effects
gen t = year - 2006
gen inter = t * mean_wp

egen stateyear_fixed = group(state year)
egen district_fixed = group(state district)

gen cum_lncapacity_turbine = log(cum_capacity_turbine + 1)
gen cum_lncount_turbine = log(cum_count_turbine + 1 )

rename env_job_ord envOrd_job
rename gw_opinion_ord gwOrd_opinion
rename ideo_self libOrd_ideo
rename d_liberal lib_ideo


*------------ Main results: By pro- or anti- environmental voting ------------*
local outcome env_job gw_opinion envOrd_job gwOrd_opinion libOrd_ideo lib_ideo // outcome variable 
local endogenous cum_capacity_turbine cum_count_turbine cum_lncapacity_turbine cum_lncount_turbine // endogenous variable
local instrument inter // instrument
local admin1_trend stateyear_fixed // geography * time trend
local admin2 district_fixed // panel unit (cluster variable)

/* List of variables:
		
  1. outcome 		- dependent variable
  2. endogenous 	- independent variable (endogeous) to be instrumented
  3. instrument 	- instrument
  4. admin1_trend 	- admin1-specific time trend (i.e. state by year fixed effect) 
  5. admin2 		- panel unit and cluster variable (i.e congressional district fixed effect)

*/

foreach y in `outcome' {
	// Create outcome variable label for storing estimates
	tokenize "`y'", parse("_")
	local y_name "`1'"
	di "`y_name'"
	
	foreach x in `endogenous' {
			// Create endogenous variable label for storing estimates
			tokenize "`x'", parse("_")
			local x_name "`3'"
			di "`x_name'"
			
			// Run IV regression
			reghdfe `y' (`x' = `instrument'), absorb(`admin1_trend' `admin2') ffirst stages(first ols reduced) vce(cluster `admin2') old
			
			// Store IV, first stage, OLS, reduced form estimates
			estimates store `y_name'_`x_name'_iv
			estimates restore reghdfe_first1
			estimates store `y_name'_`x_name'_first
			estimates restore reghdfe_ols
			estimates store `y_name'_`x_name'_ols
			estimates restore reghdfe_reduced
			estimates store `y_name'_`x_name'_reduced
		}
}

*--------------------- Export LaTeX regression tables -----------------------*
cd "$rootDir/$resultDir/Tables"
		
** Combined
esttab env_lncapacity_iv env_lncount_iv gw_lncapacity_iv gw_lncount_iv using TableA22.tex, booktabs replace ///
		refcat(cum_lncapacity_turbine "\emph{Panel A: Dummy Dependent Var}", nolabel) ///
		b(%9.3f) se noconstant noobs nonotes star(* 0.10 ** 0.05 *** 0.01) ///
		stats(N N_clust r2, labels("Observations" "Districts" "\(R^{2}\)") fmt(0 0 2)) /// 
		varlabels(cum_lncapacity_turbine "log(Cumulative capacity+1)" cum_lncount_turbine "log(Cumulative count+1)") varwidth(20) modelwidth(10) ///
		mtitles("Model" "Model" "Model" "Model") ///
		mgroups("Environment v. Job" "Global Warming", pattern(1 0 1 0) prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span})) ///
		width(\hsize)
		
esttab envOrd_lncapacity_iv envOrd_lncount_iv gwOrd_lncapacity_iv gwOrd_lncount_iv using TableA22.tex, booktabs append ///
		refcat(cum_lncapacity_turbine "\emph{Panel B: Ordinal Dependent Var}", nolabel) ///
		nomtitles se noconstant nonotes legend nonumbers collabels(none) star(* 0.10 ** 0.05 *** 0.01) ///
		b(%9.3f) stats(N N_clust r2, labels("Observations" "Districts" "\(R^{2}\)") fmt(0 0 2)) ///
		varlabels(cum_lncapacity_turbine "log(Cumulative capacity+1)" cum_lncount_turbine "log(Cumulative count+1)") varwidth(20) modelwidth(10) ///
		width(\hsize)

*******************************************************************************
/*                	     			TABLE A7		 		  			     */
*******************************************************************************

** Ideology
// Reduced Form
esttab lib_lncapacity_reduced lib_lncount_reduced libOrd_lncapacity_reduced libOrd_lncount_reduced using TableA7.tex, booktabs replace /// 
		refcat(inter "\emph{Panel A: Reduced Form}", nolabel) ///
		b(%9.3f) se noconstant noobs nonotes star(* 0.10 ** 0.05 *** 0.01) ///
		varlabels(inter "Mean wind potential * time") varwidth(20) modelwidth(10) ///
		mtitles("Model" "Model" "Model" "Model") ///
		mgroups("Liberal (\%)" "Ideology (5-Point Scale)", pattern(1 0 1 0) prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span})) ///
		width(\hsize)
		
// OLS
esttab lib_lncapacity_ols lib_lncount_ols libOrd_lncapacity_ols libOrd_lncount_ols using TableA7.tex, booktabs append /// 
		refcat(cum_lncapacity_turbine "\emph{Panel B: OLS}", nolabel) ///
		b(%9.3f) nomtitles se noconstant noobs nonotes nonumbers collabels(none) star(* 0.10 ** 0.05 *** 0.01) ///
		varlabels(cum_lncapacity_turbine "log(Cumulative capacity+1)" cum_lncount_turbine "log(Cumulative count+1)") varwidth(20) modelwidth(10) ///
		width(\hsize)

// IV
esttab lib_lncapacity_iv lib_lncount_iv libOrd_lncapacity_iv libOrd_lncount_iv using TableA7.tex, booktabs append ///
		refcat(cum_lncapacity_turbine "\emph{Panel C: IV}", nolabel) /// 
	    nomtitles se noconstant nonotes legend nonumbers collabels(none) star(* 0.10 ** 0.05 *** 0.01) ///
		b(%9.3f) stats(N N_clust, labels("Observations" "Districts") fmt(0 0)) /// 
		varlabels(cum_lncapacity_turbine "log(Cumulative capacity+1)" cum_lncount_turbine "log(Cumulative count+1)") varwidth(20) modelwidth(10) ///
		width(\hsize)

		
** Close log file
log close
cd "$rootDir/$logDir"
translate 009_analysis_CCES.smcl 009_analysis_CCES.log, replace


